Imports System
Imports System.Xml

''CONVERTED

Public Class ProductsMsg
	Inherits BaseMsg

	'<CPCAPI v="1.1">
	'      <ProdReq ReqID="testID"/>
	'</CPCAPI>

	'## Product Request Response
	'<CPCAPI v="1.1">
	'      <ProdRsp ReqID="testID">
	'            <Prod ID="FI" LongName="1% Fuel Oil NWE Crack Spread Swap " ShortName="Fuel Oil NWE Crack v.ICE" DPL="2" MinPriceTick="0.01" QuantityBlock="1000" UOM="Barrels" SubTyp="MN" Group="Petroleum Crack Spreads" CFI="FXXXX">
	'                  <TmUnit>mo</TmUnit>
	'            </Prod>
	'            <Prod ID="FK" LongName="3.5% Fuel Oil (Rdam) v. 3.5% FOB MED Spread Swap " ShortName="Euro 3.5% Fuel Oil Spread" DPL="2" MinPriceTick="0.01" QuantityBlock="1000" UOM="Metric tons" SubTyp="MN" Group="Petroleum Products" CFI="FXXXX">
	'                  <TmUnit>mo</TmUnit>
	'            </Prod>

    '## CPC 2.0 response
    '<FIXML>
    '   <Batch TotMsg="2">
    '       <Hdr SID="CME" SSub="CPAPI" TID="..." TSub="..."/>
    '       <SecDef RspTyp="100" ReqID="..." DizDt="" RptID="..." ReqRslt="0" Txt="Valid Request">
    '           <Instrmt ID="CL" Src="H" SecTyp="FUT"... />
    '           <InstrmtExt>
    '               <Attrb Typ="24".../>
    '           </InstrmtExt>
    '           <ProdClsfnGrp>
    '               <ProdClsfn Rsn="7" Val="..."/>
    '           </ProdClsfnGrp>
    Public Sub New()
        RequestType = "SecDefReq"
        ResponseType = "Batch"
        ReqTyp = "100"
    End Sub

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

		Return True
	End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

		Dim prod As XmlNode

        For Each prod In node.ChildNodes
            If prod.Name = "SecDef" Then
                If prod.Attributes("ReqRslt").InnerText = "0" Then
                    InsertUpdateProd(prod)
                End If
            End If
        Next

	End Sub

	Private Sub InsertUpdateProd(ByRef prod As XmlNode)
        Dim child As XmlNode
        Dim instr As XmlNode

		Dim id As String
		Dim longname As String
		Dim shortname As String
		Dim dpl As String
		Dim minpricetick As String
		Dim qtyblock As String
		Dim uom As String
		Dim subtype As String
        Dim group As String = ""
        Dim cfi As String
        Dim sectyp As String
        Dim unit As String
        Try

            'id = prod.Attributes("ID").InnerText
            'longname = prod.Attributes("LongName").InnerText
            'shortname = prod.Attributes("ShortName").InnerText
            'dpl = prod.Attributes("DPL").InnerText
            'minpricetick = prod.Attributes("MinPriceTick").InnerText
            'qtyblock = prod.Attributes("QuantityBlock").InnerText
            'uom = prod.Attributes("UOM").InnerText
            '      subtype = "" 'prod.Attributes("SubTyp").InnerText
            'group = prod.Attributes("Group").InnerText
            'cfi = prod.Attributes("CFI").InnerText

            instr = prod.ChildNodes(0) '  should be Instrmt node

            id = getAttr(instr, "ID")
            longname = getAttr(instr, "Desc")
            shortname = ""
            dpl = ""
            minpricetick = getAttr(instr, "MinPxIncr")
            qtyblock = getAttr(instr, "UOMQty")
            uom = getAttr(instr, "UOM")
            subtype = getAttr(instr, "ProdTerm")

            cfi = getAttr(instr, "SecTyp")
            sectyp = getAttr(instr, "SecTyp")
            unit = getAttr(instr, "TmUnit")

            For Each child In prod.ChildNodes
                If child.Name = "InstrmtExt" Then
                    InsertProdUnits(child, id)
                ElseIf child.Name = "ProdClsfnGrp" Then
                    group = getAttr(child.ChildNodes.Item(0), "Val")
                End If
            Next

            Dim dbi As dbInterface = Globals.dbi

            dbi.SetupStoredProc("sp_nymx_insupd_product")
            dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, id)
            dbi.AddStoredProcParam("@long_name", SqlDbType.VarChar, longname)
            dbi.AddStoredProcParam("@short_name", SqlDbType.VarChar, shortname)
            dbi.AddStoredProcParam("@dpl", SqlDbType.VarChar, dpl)
            dbi.AddStoredProcParam("@min_price_tick", SqlDbType.VarChar, minpricetick)
            dbi.AddStoredProcParam("@quantity_block", SqlDbType.VarChar, qtyblock)
            dbi.AddStoredProcParam("@uom", SqlDbType.VarChar, uom)
            dbi.AddStoredProcParam("@subtyp", SqlDbType.VarChar, subtype)
            dbi.AddStoredProcParam("@prod_group", SqlDbType.VarChar, group)
            dbi.AddStoredProcParam("@cfi", SqlDbType.VarChar, cfi)
            dbi.AddStoredProcParam("@unit", SqlDbType.VarChar, unit)
            dbi.AddStoredProcParam("@sectyp", SqlDbType.VarChar, sectyp)

            dbi.RunStoredProc()
        Catch ex As Exception

        End Try

	End Sub

    Private Function getAttr(ByRef node As XmlNode, ByVal val As String) As String
        If node.Attributes(val) Is Nothing Then
            Return ""
        End If

        Return node.Attributes(val).InnerText().ToString()
    End Function

    Private Sub InsertProdUnits(ByRef prod As XmlNode, ByVal id As String)
        Dim dbi As dbInterface = Globals.dbi

        Try

            'Dim id As String
            Dim utype As String

            Dim unit As XmlNode
            'id = prod.Attributes("ID").InnerText

            'dbi.nonquery("delete from nymx_prod_units where prod_id=" & dbi.quote(id))
            dbi.nonquery("delete from nymx_prod_subtypes where prod_id=" & dbi.quote(id))

            For Each unit In prod.ChildNodes
                'If unit.Name = "TmUnit" Then
                '	utype = unit.InnerText

                '	dbi.SetupStoredProc("sp_nymx_ins_product_unit")
                '	dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, id)
                '	dbi.AddStoredProcParam("@unit", SqlDbType.VarChar, utype)
                '	dbi.RunStoredProc()
                '         Else
                If unit.Attributes("Typ").InnerText = "24" Then
                    utype = getAttr(unit, "Val")

                    dbi.SetupStoredProc("sp_nymx_ins_product_subtype")
                    dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, id)
                    dbi.AddStoredProcParam("@unit", SqlDbType.VarChar, utype)
                    dbi.RunStoredProc()
                End If
            Next
        Catch ex As Exception

        End Try

    End Sub

End Class
